package cn.edu.scau.cmi.crawler.wrapper;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.edu.scau.cmi.crawler.service.CrawlerdetectImportService;
import cn.edu.scau.cmi.crawler.service.CrawlerImportService;
import cn.edu.scau.cmi.ema.controller.base.CmiEmaController;
import cn.edu.scau.cmi.ema.dao.CrawlerfilecolumnDAO;
import cn.edu.scau.cmi.ema.domain.Crawlerdetect;
import cn.edu.scau.cmi.ema.domain.Crawlerfile;
import cn.edu.scau.cmi.ema.domain.Crawlerfilecolumn;
import cn.edu.scau.cmi.ema.domain.Crawlerfilestandardcolumn;
import cn.edu.scau.cmi.ema.domain.Crawlerfilestandardetail;
import cn.edu.scau.cmi.ema.service.base.EntityFactoryService;
import cn.edu.scau.cmi.ema.util.ExcelUtil;

@Service
public class CrawlerdetectWrapper extends CmiEmaController{
	@Autowired EntityFactoryService entityFactory;
	@Autowired CrawlerfilecolumnDAO crawlerfilecolumnDAO;
	@Autowired CrawlerdetectImportService crawlerdetectImportService;
	@Autowired CrawlerImportService importService;

	public CrawlerdetectWrapper() {

	}
	
//	把文件的物理列-》标准逻辑列-》
	public Crawlerdetect wrapCrawlerdetect(Crawlerfile crawlerfile, Sheet sheet, Row row){
		
		HashMap<String, Integer> crawlerfilePhysicsColumn2IndexMap = new HashMap<String, Integer>();
		
//		(1) 文件的标准列map，编号=2，描述=8......
		Crawlerdetect crawlerdetect = entityFactory.initCrawlerdetect();
		
		Row titleRow = importService.getTitleRow(sheet);
		
		short lastCellNum = titleRow.getLastCellNum();
		
		for(int cellNum = 0 ; cellNum < lastCellNum; cellNum++) {
			Cell cell = titleRow.getCell(cellNum);
			String cellValue = ExcelUtil.getCellValue(cell);
//			！！！有可能最后列是没有内容 ，但是。。。。。。被读取。。。。
			if(cellValue.equals("")) {
				continue;
			}
			crawlerfilePhysicsColumn2IndexMap.put(cellValue, cellNum);
		}
		
//		(2) 标准列与物理列的map
		Set<Crawlerfilestandardetail> standardetails = crawlerfile.getCrawlerfilestandard().getCrawlerfilestandardetailsForCrawlerfilestandard();
		Map<Crawlerfilestandardcolumn, Integer> crawlerfilestandardcolumn2PhysicsIndex = new HashMap<Crawlerfilestandardcolumn, Integer>();
//		所有的标准列的名称-》物理列-》物理列位置
		for(Crawlerfilestandardetail standarDetail : standardetails) {
			Crawlerfilestandardcolumn standardcolumn = standarDetail.getCrawlerfilestandardcolumn();
//			物理名称：检验结果  标准的中文名：检测结果   
//			这个标准列的文件物理列是？？？
			Set<Crawlerfilecolumn> physicscolumns = standarDetail.getCrawlerfilestandardcolumn().getCrawlerfilecolumnsForCrawlerfilestandardcolumn();
			
			for(Crawlerfilecolumn physicsColumn:physicscolumns) {
				String pyhsicaColumnName = physicsColumn.getName();
				
				Integer physicsIndex = crawlerfilePhysicsColumn2IndexMap.get(pyhsicaColumnName);
				if(physicsIndex !=null) {
					crawlerfilestandardcolumn2PhysicsIndex.put(standardcolumn, physicsIndex);
				}
			 }
		}
		
//		(3) 所有的标准列赋值
		
		Set<Entry<Crawlerfilestandardcolumn, Integer>> entrySet = crawlerfilestandardcolumn2PhysicsIndex.entrySet();
		
		for(Entry<Crawlerfilestandardcolumn, Integer> entry: entrySet) {
//			
//			brand:2, description:10
			Crawlerfilestandardcolumn standardcolumn = entry.getKey();
			Integer physicxIndex = entry.getValue();
//			standardcolumn.setName(ExcelUtil.getCellString(row, physicxIndex));
			String standardcolumValue = ExcelUtil.getCellString(row, physicxIndex);
			
//			利用反射设置crawlerevent的值或者使用固定的方式设置值。
			Method methond;
			try {
				String getMethodName = "set" + standardcolumn.getName().substring(0, 1).toUpperCase() + standardcolumn.getName().substring(1);
				methond = Crawlerdetect.class.getDeclaredMethod(getMethodName, String.class);
				methond.invoke(crawlerdetect, standardcolumValue);
			} catch (NoSuchMethodException | SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
//		或者采用静态的方法
		return crawlerdetect;
	}	
}
